package org.dreamfly.core.action;

import net.sf.json.JSONArray;
import org.dreamfly.core.util.ChangeChinese;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-9-20
 * Time: 12:59:47
 * To change this template use File | Settings | File Templates.
 */
public class FileActionSupport extends BaseAction {
    protected List<File> files;//上传的文件
    protected List<String> filesFileName; //上传文件的名字
    protected List<String> filesContentType;//上传文件的类型
    protected String filePath;//文件上传后的相对路径，下载路径
    protected List<String> errorList = new ArrayList<String>();//删除失败的文件的路径
    protected String[] filePaths;//要删除的文件的路径名

    /**
     * 默认下载文件
     *
     * @return the name of the file to be downloaded
     */
    @Override
    public String execute() {
        if (this.filePath != null) {
            return download();
        } else {
            request.setAttribute("msg", "请确定文件正确上传并重新上传文件");
            return ERROR;
        }
    }

    /**
     * 指定路径下载文件
     *
     * @return
     */
    public String download() {
        if (download(filePath) == null) {
            request.setAttribute("msg", "请确定文件正确上传并重新上传文件");
            return ERROR;
        } else {
            return SUCCESS;
        }
    }

    /**
     * 指定文件路径删除
     *
     * @return
     */
    public String delete() throws Exception {
        if (null != filePaths) {
            for (String filepath : filePaths) {
                if (!UploadFile.fileRemove(filepath))
                    this.errorList.add(filepath);
            }
            return (null == errorList || 0 == errorList.size()) ? SUCCESS : INPUT;
        }
        return ERROR;
    }

    /**
     * 文件上传使用
     *
     * @param folder    上传文件目录
     * @param className
     * @param isTemp
     * @throws UnsupportedEncodingException
     */
    protected void upload(UploadFile.UploadFolder folder, String className, Boolean isTemp) {
        for (int i = 0; i < files.size(); i++) {
            String name = null;
            try {
                if (isTemp) {
                    name = ChangeChinese.changeFilePath(filesFileName.get(i));
                } else {
                    name = new String(filesFileName.get(i).getBytes("UTF-8"), "UTF-8");
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String fileName = UploadFile.fileUpload(files.get(i), name, folder, className, isTemp);
            System.out.println("fileName:" + fileName);
            filesFileName.set(i, fileName);
        }
    }

    /**
     * 下载文件使用
     *
     * @param relativeFilePath 下载相对路径
     * @return 文件名
     */
    protected String download(String relativeFilePath) {
        return UploadFile.fileDownload(response, relativeFilePath);
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }

    public void setFilesFileName(List<String> filesFileName) {
        this.filesFileName = filesFileName;
    }

    public void setFilesContentType(List<String> filesContentType) {
        this.filesContentType = filesContentType;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setErrorList(List<String> errorList) {
        this.errorList = errorList;
    }

    public String getInfoErrors() {
        return JSONArray.fromObject(this.errorList).toString();
    }

    public List<File> getFiles() {
        return files;
    }

    public String[] getFilePaths() {
        return filePaths;
    }

    public void setFilePaths(String[] filePaths) {
        this.filePaths = filePaths;
    }
}


